package com.onjob.recruit;

import java.sql.SQLException;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;

import util.dao.HibernateDaoImpl;

public class RecruitDaoImpl extends HibernateDaoImpl<Recruit, String> implements
		RecruitDao {

	public void deleteRecruitFindByCompany(final Company company) {
		StringBuffer hql = new StringBuffer();
		hql.append("delete from Recruit r where 1=1 ");
		hql.append("and r.companyId.id=:companyId ");
		final String ss = new String(hql);
		this.getHibernateTemplate().execute(new HibernateCallback() {

			@Override
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				Query query = session.createQuery(ss);
				query.setString("companyId", company.getId());
				query.executeUpdate();
				return null;
			}

		});
	}

	@SuppressWarnings("unchecked")
	public List<Recruit> findByCity(final String cityName) {
		StringBuffer hql = new StringBuffer();
		hql.append("from Recruit r where 1=1 ");
		hql.append("and r.cityId.cityName=:cityName ");
		final String s=new String(hql);
		return (List<Recruit>)this.getHibernateTemplate().execute(new HibernateCallback() {	
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				
				Query q = session.createQuery(s);
				q.setString("cityName", cityName);
				return q.list();
			}
		});
	}
	
	@SuppressWarnings("unchecked")
	public List<Recruit> findByCompany(final String companyId){
		return (List<Recruit>)this.getHibernateTemplate().execute(new HibernateCallback() {	
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				System.out.println(companyId+"???????????????????????????");
				return session.createQuery("from Recruit r where r.companyId.id="+companyId).list();
			}
		});
	}
	
	@SuppressWarnings("unchecked")
	public boolean isCompanyHaveRecruit(final Company company){
		StringBuffer hql = new StringBuffer();
		hql.append("from Recruit r where 1=1 ");
		hql.append("and r.companyId.id=:companyId ");
		final String s2 = new String(hql);
		List<Recruit> list = (List<Recruit>)this.getHibernateTemplate().execute(
				new HibernateCallback(){

					public Object doInHibernate(Session session)
							throws HibernateException, SQLException {
						Query query = session.createQuery(s2);
						query.setString("companyId", company.getId());
						return query.list();
					}
					
				}
		);
		if(list.size()!=0){
			return true;
		}else{
			return false;
		}
		
	}
}
